<?php

session_start();

// Truy cập các trang mà chưa login -> redirect /login
if (!isset($_SESSION["username"]) && $_SERVER["REQUEST_URI"] != "/login") {
    header("Location: /login");
}

// Đã login mà vô trang login -> redirect homepage
if (isset($_SESSION["username"]) && $_SERVER["REQUEST_URI"] == "/login") {
    header("Location: /");
}


define("SMARTY_DIR", "c:/wamp/www/Project_2014-08-22/libs/Smarty-3.1.19/libs/");
define("ROOT_DIR", "c:/wamp/www/Project_2014-08-22/mpcamera/");
define("WEB_DIR", ROOT_DIR . "htdocs/");
define("EXCEL_DIR", "c:/wamp/www/Project_2014-08-22/libs/PHPExcel/");
define("MAILER_DIR", "c:/wamp/www/Project_2014-08-22/libs/PHPMailer/");

define("SMTPDEBUG", 0);
define("SMTPAUTH", true);
define("SMTPSECURE", "");

define("HOST", "mail.targetmedia.vn");
define("PORT", 25);

define("FROMNAME", "Target Media");
define("FROM", "no-reply@targetmedia.vn");
define("USERNAME", "no-reply@targetmedia.vn");
define("PASSWORD", "P@ssw0rd");
define("CHARSET", "UTF-8");


require(SMARTY_DIR . "Smarty.class.php");
require(EXCEL_DIR . "PHPExcel.php");
require(MAILER_DIR . "class.phpmailer.php");

class Smarty_MPCamera extends Smarty {

    function __construct() {
        parent::__construct();
        $this->setTemplateDir(ROOT_DIR . "templates");
        $this->setCompileDir(ROOT_DIR . 'templates_c');
        $this->setConfigDir(ROOT_DIR . 'configs');
        $this->setCacheDir(ROOT_DIR . 'cache');
        //$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
        $this->assign('app_name', 'MPCamera');
        //$this->testInstall();
        //$this->debugging = true;
        //
        // Nếu đã login thì assign username
        if (isset($_SESSION["username"])) {
            $this->assign("username", $_SESSION["username"]);
        }

        $connect = mysql_connect("localhost", "root", "", true)
                or die("Connection error!");
        mysql_select_db("payment_request")
                or die("Database not found!");
        mysql_set_charset('utf8', $connect);
    }

    // Loại bỏ kí tự đặc biệt, nếu number = true thì sẽ chỉ lấy number
    function clean($string, $number = false) {
        $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
        $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
        $string = preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
        if ($number == true) {
            $string = preg_replace('/[^0-9]/', '', $string);
        }
        return $string;
    }

    function begin_transaction() {
        mysql_query("BEGIN");
    }

    function commit() {
        mysql_query("COMMIT");
    }

    function rollback() {
        mysql_query("ROLLBACK");
    }

# sale

    function getSaleViews() {
        $query = "
            SELECT `MaDD`,`TrafficType`,`TenDD`,`DiaChi`,SUBSTR(`TenQH`,6) AS `TenQH`,
                    CASE WHEN `TenTP` = 'Hồ Chí Minh' THEN 'HCM' WHEN `TenTP` = 'Hà Nội' THEN 'HN' END AS `TenTP`,
                    `QuantityTable`,`QuantityLCD`,`PeopleWeek`,`PeopleDay`,`ViewerDay`
            FROM `diadiem`, `diachi`, `quanhuyen`, `thanhpho`, saleinfo
            WHERE `diadiem`.`MaDC` = `diachi`.`MaDC` AND `diachi`.`MaQH` = `quanhuyen`.`MaQH` AND `diachi`.`MaTP` = `thanhpho`.`MaTP` AND `diadiem`.`MaSI` = `saleinfo`.`MaSI`
            ;
        ";
        $result = mysql_query($query);
        while ($row = mysql_fetch_array($result)) {
            $saleView[] = $row;
        }
        return $saleView;
    }

    function setSaleEdit($maSI, $cbbTrafficType, $txtQuantityTable, $txtQuantityLCD, $txtPeopleWeek, $txtPeopleDay, $txtViewerDay) {
        $query = "
            UPDATE `saleinfo`
            SET
              `TrafficType` = '$cbbTrafficType',
              `QuantityTable` = $txtQuantityTable,
              `QuantityLCD` = $txtQuantityLCD,
              `PeopleWeek` = $txtPeopleWeek,
              `PeopleDay` = $txtPeopleDay,
              `ViewerDay` = $txtViewerDay
            WHERE `MaSI` = $maSI;
        ";
        $result = mysql_query($query);
        return $result;
    }

    function getSaleItemByID($id) {
        $query = "
            SELECT `saleinfo`.`MaSI`,`TenDD`,`TrafficType`,`QuantityTable`,`QuantityLCD`,`PeopleWeek`,`PeopleDay`,`ViewerDay`
            FROM `diadiem`, `saleinfo`
            WHERE `diadiem`.`MaSI` = `saleinfo`.`MaSI` AND
                  `MaDD`= $id
            ;
        ";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        return $row;
    }

# End - sale
}

class PHPMailer_Target extends PHPMailer {

    function __construct() {
        parent::__construct();

        $this->isSMTP();
        $this->SMTPDebug = SMTPDEBUG;
        $this->SMTPAuth = SMTPAUTH;
        $this->SMTPSecure = SMTPSECURE;

        $this->Host = HOST;
        $this->Port = PORT;

        $this->isHTML(true);

        $this->FromName = FROMNAME;
        $this->From = FROM;
        $this->Username = USERNAME;
        $this->Password = PASSWORD;

        $this->CharSet = CHARSET;
    }

}
